﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CamShared;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Diagnostics;
using DavuxLib;

namespace Screen
{
    public class ScreenConn :ICameraConnector
    {
    

        public void DetectCameras()
        {
            List<ICamera> Cams = new List<ICamera>();
            foreach (System.Windows.Forms.Screen s in System.Windows.Forms.Screen.AllScreens)
            {
                CameraDiscovered(new ScreenCamera(s), this);
            }
        }

        public ICamera CreateCamera(string Name, string Class, string[] args)
        {
            return null;
        }

        public string Name
        {
            get { return "Screen"; }
        }



        public void Dispose()
        {

        }

        public event CamShared.CamShared.CameraDiscovered CameraDiscovered;

    }

    public class ScreenCamera : BaseCamera
    {
        Thread t = null;


        public override bool ShowConfig(IntPtr handle) { return false; }



        System.Windows.Forms.Screen Screen = null;
        public ScreenCamera(System.Windows.Forms.Screen s)
        {
            Screen = s;
            Name = s.DeviceName;
            Class = "Computer Monitor";

            Settings.Get(Name + "|Width", s.Bounds.Width);
            Settings.Get(Name + "|Height", s.Bounds.Height);
            Settings.Get(Name + "|Recording Width", s.Bounds.Width);
            Settings.Get(Name + "|Recording Height", s.Bounds.Height);
        }

        private void ThreadStart()
        {
            while (true)
            {
                try
                {
                    // Trace.WriteLine("Getting Screencap");
                    Bitmap bmpScreenshot = new Bitmap(Screen.Bounds.Width, Screen.Bounds.Height, PixelFormat.Format32bppArgb);
                    using (Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot))
                    {
                        gfxScreenshot.CopyFromScreen(Screen.Bounds.X, Screen.Bounds.Y, 0, 0, Screen.Bounds.Size, CopyPixelOperation.SourceCopy);
                    }
                    //Trace.WriteLine("Got Screencap null: " + bmpScreenshot == null);
                    FrameArrived(this, bmpScreenshot);
                }
                catch (ThreadAbortException)
                {
                    // we know.
                }
                catch (System.Exception ex)
                {
                    Trace.WriteLine("Screen: Couldn't capture: " + ex);
                }
                int s = Settings.Get(this.Name + "|Sample Rate (ms)", 250);
                if (s < 20)
                {
                    s = 20;
                }
                Thread.Sleep(s);
            }
        }

        protected override void OnActivated()
        {
            t = new Thread(ThreadStart);
            t.Start();
        }

        protected override void OnDeActivated()
        {
            t.Abort();
            t = null;
        }
    }

}
